package cn.xhb.controller;

import cn.xhb.domain.Content;
import cn.xhb.domain.User;
import cn.xhb.service.ContentService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/content")
public class CommentController {
    @Resource
    private ContentService contentService;
    @RequestMapping("/save")
    public String save(Content content,HttpSession httpSession){
        User user = (User) httpSession.getAttribute("user");
        if (user==null){
            return "login";
        }else{
            content.setUserId(user.getId());
            contentService.save(content);
            return "redirect:/content/list";
        }

    }
    @RequestMapping("/list")
    public ModelAndView list(HttpSession httpSession){
        ModelAndView modelAndView=new ModelAndView();
        User user = (User) httpSession.getAttribute("user");
          if (user==null){
          modelAndView.setViewName("login");
          }else {
              List<Content> contentList=contentService.findAll(user.getId());
              modelAndView.addObject("contentList",contentList);
              modelAndView.setViewName("list");
          }

        return modelAndView;
}
    @RequestMapping("/del/{id}")
    public String del(@PathVariable("id") int id){
        contentService.del(id);
        return "redirect:/content/list";
    }
    @RequestMapping("/findAll")
    public ModelAndView findAll(){
    ModelAndView modelAndView=new ModelAndView();
        List<Content> contentList = contentService.find();
        modelAndView.addObject("contentList",contentList);
        modelAndView.setViewName("allList");
        return modelAndView;
    }

}
